package com.ww.yaodao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ww.yaodao.model.bo.ProductDO;
import com.ww.yaodao.service.FileUploadService;
import com.ww.yaodao.service.ProductService;
import com.ww.yaodao.util.BaseResult;
import com.ww.yaodao.util.ResultEnums;
import com.ww.yaodao.model.vo.ProductPageVO;
import com.ww.yaodao.model.vo.ProductVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 库存模块
 */

@RestController
@RequestMapping("product")
@Api(tags = "商品模块")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询所有药品
     *
     * @return
     */
    @GetMapping("queryproduct")
    @ApiOperation("无条件查询所有药品")
    public BaseResult<List<ProductDO>> query() {
        List<ProductDO> plist = productService.queryAllProduct();
        return BaseResult.success(plist);
    }

    /**
     * 更新药品
     *
     * @param productVo
     * @return
     */
    @PostMapping("updateproduct")
    @ApiOperation("更新药品")
    public BaseResult<Integer> update(@RequestBody ProductVO productVo) {
        int row = productService.updateProduct(productVo);
        if (row != 0) {
            return BaseResult.success(ResultEnums.UPDATED);
        } else {
            return BaseResult.error(ResultEnums.UPDATE_ERROR);
        }
    }

    /**
     * 删除药品（根据id）
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id删除药品")
    @ApiResponses({
            @ApiResponse(code = 1004, message = "删除失败"),
            @ApiResponse(code = 204, message = "删除成功")
    })
    public BaseResult<Integer> delete(@PathVariable("id") @ApiParam("药品id") Integer id) {
        int row = productService.deleteProductById(id);
        if (row != 0) {
            return BaseResult.success(ResultEnums.DELETED);
        } else {
            return BaseResult.error(ResultEnums.DELETE_ERROR);
        }
    }


    /**
     * 分页查询
     *
     * @param current
     * @param size
     * @param productVo
     * @return
     */
    @GetMapping("/allProductInfo")
    @ApiOperation(value = "分页查询")
    public Page<ProductDO> page(@ApiParam(value = "当前页码") Integer current,
                                @ApiParam(value = "页容量") Integer size,
                                ProductPageVO productVo) {
        Page<ProductDO> page = productService.pageProduct(current, size, productVo);
        return page;
    }

}
